package com.wwwzh.himalayafm.adapter.ranking;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.wwwzh.himalayafm.R;
import com.wwwzh.himalayafm.activity.XmlyActivity;
import com.wwwzh.himalayafm.entity.ranking.DisRankEntity;
import com.wwwzh.himalayafm.entity.ranking.DisRankItemContent;


/**
 * Created by wen10 on 2016/12/5.
 */

public class DisRankItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private Context context;
    private DisRankEntity data;
    public DisRankItemAdapter(Context context, DisRankEntity data) {
        this.context = context;
        this.data = data;
    }




    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(context).inflate(viewType, parent, false);
        RecyclerView.ViewHolder holder = null;
        switch (viewType){
            case R.layout.layout_disranking_item:
                holder = new ViewHolderImg(rootView);
                break;
            case R.layout.layout_disranking_item2:
                holder = new ViewHolderContent(rootView);
                break;
        }

        return holder;

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position == 0){//0位置是显示头视图
            DisRankItemAdapter.ViewHolderImg holderImg = (ViewHolderImg) holder;
            Picasso.with(context).load(data.getFocusImages().getList().get(0).getPic()).into(holderImg.rankHead);
            holderImg.rankHead.setOnClickListener(this);
        } else {
            //其他位置是显示内容视图
            DisRankItemAdapter.ViewHolderContent holderContent = (ViewHolderContent) holder;
            if ( position == 1|| position == 17) {
                //1和17位置显示内容标题
                holderContent.disRankItemTitle.setVisibility(View.VISIBLE);
                 if (position == 1) {
                     holderContent.rankName.setText(data.getDatas().get(0).getTitle());
                 } else {
                     holderContent.rankName.setText(data.getDatas().get(1).getTitle());
                 }
             }else {
                //隐藏内容标题
                holderContent.disRankItemTitle.setVisibility(View.GONE);
            }


            //以下为内容数据填充
            final DisRankItemContent itemContent;
            if (position <17){

                itemContent   = data.getDatas().get(0).getList().get(position-1);
            }else {
                itemContent   = data.getDatas().get(1).getList().get(position-17);
            }
            holderContent.disRankItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context,itemContent.getTitle(),Toast.LENGTH_SHORT).show();
                }
            });
            holderContent.itemContentTitle.setText(itemContent.getTitle());
            Picasso.with(context).load(itemContent.getCoverPath()).into(holderContent.itemContentImg);
            holderContent.itemContentCon1.setText("1 "+itemContent.getFirstKResults().get(0).getTitle());
            holderContent.itemContentCon2.setText("2 "+itemContent.getFirstKResults().get(1).getTitle());
        }
    }

    @Override
    public int getItemCount() {
        int itemCount = 1;
        for (int i = 0;i<data.getDatas().size();i++){
            itemCount += data.getDatas().get(i).getCount();
        }


        return itemCount;
    }

    @Override
    public int getItemViewType(int position) {
        //根据位置返回ViewType
        int viewType ;
        if (position == 0){
            viewType = R.layout.layout_disranking_item;
        }else {
            viewType = R.layout.layout_disranking_item2;
        }
        return viewType;

    }

    //头视图的点击事件
    @Override
    public void onClick(View v) {
        XmlyActivity activity = (XmlyActivity) context;
        activity.ondisRankClicked(data.getFocusImages().getList().get(0));
    }

    public class ViewHolderImg extends RecyclerView.ViewHolder{
        private ImageView rankHead;

        public ViewHolderImg(View itemView) {
            super(itemView);
            rankHead = (ImageView) itemView.findViewById(R.id.DisRank_IV);
        }
    }

    public class ViewHolderContent extends RecyclerView.ViewHolder{
        private TextView rankName;
        private RelativeLayout disRankItem,disRankItemTitle;
        private ImageView itemContentImg;
        private TextView itemContentTitle,itemContentCon1,itemContentCon2;
        public ViewHolderContent(View itemView) {
            super(itemView);
            rankName = (TextView) itemView.findViewById(R.id.DisRankHeaderName_TV);
            disRankItem = (RelativeLayout) itemView.findViewById(R.id.DisRankItem_RL);
            disRankItemTitle = (RelativeLayout) itemView.findViewById(R.id.DisRankItemTitle_RL);
            itemContentImg = (ImageView) itemView.findViewById(R.id.DisRankContentTitle_IV);
            itemContentTitle = (TextView) itemView.findViewById(R.id.DisRankContentTitle_TV);
            itemContentCon1 = (TextView) itemView.findViewById(R.id.DisRankContent1_TV);
            itemContentCon2 = (TextView) itemView.findViewById(R.id.DisRankContent2_TV);
        }
    }


}
